var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
const { jwtAuth } = require('tools-factory-tang/jwt');

require("./db/index");//执行db的index.js文件


// 引入路由
var indexRouter = require('./routes/index');//引入关于电影的路由
var schedulesRouter = require('./routes/schedules');    //引入schedules的路由

//引入关于电影的路由
var moviesRouter = require('./routes/movies');
// 影院路由
var cinemasRouter = require('./routes/cinemas');
// 关于影厅的路由
var theatersRouter = require('./routes/theaters');
// 关于用户的路由
var usersRouter = require(`./routes/users`);
// 管理员的路由
var adminRouter = require(`./routes/admin`);
// 门户客户的路由
var orderRouter = require(`./routes/order`);


// 关于订单的路由
var orderRouter = require(`./routes/order`);

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
require("./swagger")(app);


// 添加api
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

// 引入token验证，平常是false关闭不验证状态
app.use(jwtAuth);   
app.use('/', indexRouter);

//挂载路由
app.use('/schedules', schedulesRouter);
//电影的路由
app.use("/movies", moviesRouter)
// app.use("/movies", moviesRouter);
app.use("/cinemas", cinemasRouter);
// 影厅的路由
app.use("/theaters", theatersRouter);
//用户
app.use("/users", usersRouter);
// admin管理员
app.use("/admin", adminRouter);

app.use("/order", orderRouter);




//订单
// app.use("/order", orderRouter);

app.use(function (req, res, next) {
  next(createError(404));
});

app.use(function (err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};
  // render the error page
  res.status(err.status || 500);
  res.render('error');
});
module.exports = app;
